package com.web.cms.pan.util;

import com.blue.core.util.NumberUtil;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.cms.pan.message.FileDataRequest;
import com.web.cms.pan.message.FileDataResponse;
import com.web.cms.pan.model.FileData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件数据转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
public class FileDataConvertor
{
	private FileDataConvertor()
	{
	}
	
	public static FileData toFileData(FileDataRequest request)
	{
		if (request == null)
			return null;
		
		FileData fileData = new FileData();
		fileData.setId(request.getId());
		fileData.setUserId(request.getUserId());
		fileData.setName(request.getName());
		fileData.setKey(request.getKey());
		fileData.setMimeType(request.getMimeType());
		fileData.setCreateTime(request.getCreateTime());
		fileData.setCount(request.getCount());
		fileData.setSize(request.getSize());
		fileData.setType(request.getType());
		return fileData;
	}
	
	public static List<FileData> toFileData(List<FileDataRequest> requestList)
	{
		List<FileData> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (FileDataRequest request : requestList)
		{
			list.add(toFileData(request));
		}
		return list;
	}
	
	public static FileDataResponse toResponse(FileData fileData, StorageClient storageClient)
	{
		if (fileData == null)
			return null;
		
		FileDataResponse response = new FileDataResponse();
		response.setId(fileData.getId());
		response.setUserId(fileData.getUserId());
		response.setName(fileData.getName());
		response.setKey(fileData.getKey());
		response.setMimeType(fileData.getMimeType());
		response.setCreateTime(fileData.getCreateTime());
		response.setCount(fileData.getCount());
		response.setSize(fileData.getSize());
		response.setType(fileData.getType());

		response.setSizeName(NumberUtil.byteFormat(fileData.getSize().intValue()));
		response.setUrl(storageClient.getUrl(Bucket.PAN, fileData.getKey()));
		return response;
	}
	
	public static List<FileDataResponse> toResponse(List<FileData> list, StorageClient storageClient)
	{
		List<FileDataResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (FileData fileData : list)
		{
			responseList.add(toResponse(fileData, storageClient));
		}
		return responseList;
	}
	
	public static Map<Integer, FileDataResponse> toResponse(Map<Integer, FileData> map, StorageClient storageClient)
	{
		Map<Integer, FileDataResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, FileData> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue(), storageClient));
		}
		return responseMap;
	}
	
	public static Page<FileDataResponse> toResponse(Page<FileData> page, StorageClient storageClient)
	{
		if (page == null)
			return null;
		
		Page<FileDataResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList(), storageClient));
		return responsePage;
	}
	
}
